package com.example.wm.utils;


import com.alibaba.fastjson.JSONObject;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

@Component
public class TsyUtils {
    @Value("${smbm.appsecret}")
    private String appsecreted;
    @Value("${smbm.appId}")
    private String appid;


    String accesstoken = null;

     public void run() {
      long timeInterval = 60000*60*2;// 两小时运行一次
         settoken tos=new settoken();
      Runnable runnable=new Runnable() {
          @Override
            public void run() {
              CloseableHttpClient httpClient = HttpClientBuilder.create().build();
              //响应模型
              CloseableHttpResponse response = null;
              String accessUrl="https://api.weixin.qq.com/cgi-bin/token?appid=wxba6a21693d6b5a99&secret=232661a82ca5a19a24d54db5d7bace0f&grant_type=client_credential";
              while (true){
                  System.out.println("hello");
                  //创建get请求
                  HttpGet httpGet = new HttpGet(accessUrl);
                  try {
                      response = httpClient.execute(httpGet);
                  } catch (IOException e) {
                      throw new RuntimeException(e);
                  }
                  //获取响应实体
                  HttpEntity responseEntity = response.getEntity();
                  String resultDemo = null;
                  try {
                      resultDemo = EntityUtils.toString(responseEntity);
                  } catch (IOException e) {
                      throw new RuntimeException(e);
                  }
                  //转换成对象，好把需要的字段对应的值拿出来
                  JSONObject jsonObject = JSONObject.parseObject(resultDemo);
                  System.out.println(jsonObject);
                  accesstoken= jsonObject.getString("access_token");
                  tos.setToken(accesstoken);


               try{
                      Thread.sleep(timeInterval);
                  }catch (InterruptedException e){
                      e.printStackTrace();
                  }
              }
          }
      };
      Thread thread=new Thread(runnable);
      thread.start();

    }
    public String getnowtime(){
        SimpleDateFormat formatter= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss ");
        Date date = new Date(System.currentTimeMillis());
        String time = formatter.format(date);
        return time;
    }
    public String getnowdate(){
        SimpleDateFormat formatter= new SimpleDateFormat("yyyy-MM-dd");
        Date date = new Date(System.currentTimeMillis());
        String time = formatter.format(date);
        return time;
    }

}

